/*
 * @Author: Hp_lucky 1063512385@qq.com
 * @Date: 2024-08-28 11:36:48
 * @LastEditors: Hp_lucky 1063512385@qq.com
 * @LastEditTime: 2024-10-08 15:50:28
 * @FilePath: \cocos_frame\assets\script\src\Basic\Model\Tips\FlyTipsPanel.ts
 */
import { _decorator, Tween, tween, UIOpacity, v3, view, Node, UITransform, Component } from 'cc';
import { LayerType } from '../../Config/BaseEnum';
import { PoolFactory } from '../../Component/PoolFactory';
import { TipsDataBase, TipsObjectType } from './TipsDataBase';
import { Task } from '../../../Util/CustomTask';
const { ccclass, property } = _decorator;

@ccclass('FlyTipsPanel')
export class FlyTipsPanel extends Component {
  panelLayer: LayerType;
  loadRes: string[];
  @property(PoolFactory)
  factory: PoolFactory = null!;

  /** 弹出提示 */
  flyTips(data: TipsDataBase) {
    const tips = this.factory.pop(data.className, data);
    switch (data.className) {
      case TipsObjectType.flyTipsRight:
        this.pushRight(tips);
        break;
      case TipsObjectType.flyTipsCenter:
        this.pushCenter(tips);
        break;
      case TipsObjectType.flyBlackTipsCenter:
        this.pushCenter(tips);
        break;
    }
  }

  private rightNodes: Node[] = [];
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private taskRight: Task.Sync<any[]> = null;
  private readonly rightConfig = {
    /** 飞出来时间 */
    outTime: 0.2,
    /** 停留时间 */
    holdTime: 1.3,
    /** 隐藏用的时间 */
    hideTime: 0.5,
    /** 向上移动用的时间 */
    moveUpTime: 0.2,
    width: 262,
    height: 99,
    /** 高度偏移 */
    deviation: 150,
  };
  private pushRight(tips: Node) {
    if (!this.taskRight || !this.taskRight.isRunning) {
      this.taskRight = Task.createSync();
    }
    this.taskRight.add((next) => {
      this.moveUpRight(next);
    });
    this.taskRight.add((next) => {
      this.flyRight(tips, next);
    });
    if (!this.taskRight.isRunning) {
      this.taskRight.start();
    }
  }

  private moveUpRight(next) {
    const len = this.rightNodes.length;
    if (len > 0) {
      const trans = view.getVisibleSize();
      let j = len;
      for (let i = 0; i < len; i++, j--) {
        if (j == 1) {
          tween(this.rightNodes[i])
            .to(this.rightConfig.moveUpTime, { position: v3(trans.width / 2, j * this.rightConfig.height + this.rightConfig.deviation) })
            .call(next)
            .start();
        } else {
          tween(this.rightNodes[i])
            .to(this.rightConfig.moveUpTime, { position: v3(trans.width / 2, j * this.rightConfig.height + this.rightConfig.deviation) })
            .start();
        }
      }
    } else {
      next();
    }
  }

  private flyRight(tips: Node, next) {
    if (tips == null) {
      next();
      return;
    }
    this.rightNodes.push(tips);
    const trans = this.node.getComponent(UITransform);
    this.node.addChild(tips);
    const uiOpacity = tips.getComponent(UIOpacity);
    Tween.stopAllByTarget(tips);
    Tween.stopAllByTarget(uiOpacity);
    uiOpacity.opacity = 255;
    tips.setPosition(trans.width / 2 + this.rightConfig.width, this.rightConfig.deviation);
    tween(tips)
      .to(this.rightConfig.outTime, { position: v3(trans.width / 2, this.rightConfig.deviation) }, { easing: 'backOut' })
      .call(() => {
        next();
      })
      .delay(this.rightConfig.holdTime)
      .call((tag) => {
        const uiO = tag.getComponent(UIOpacity);
        tween(uiO)
          .to(this.rightConfig.hideTime, { opacity: 0 })
          .call(() => {
            const temp = this.rightNodes.shift();
            if (temp) {
              this.factory.push(temp);
            }
          })
          .start();
      })
      .start();
  }

  private centerNodes: Node[] = [];
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private taskCenter: Task.Sync<any[]> = null;
  private readonly centerConfig = {
    /** 缩放 */
    scale: 0.6,
    /** 出来时间 */
    outTime: 0.2,
    /** 停留时间 */
    holdTime: 0.5,
    /** 隐藏用的时间 */
    hideTime: 0.5,
    /** 向上移动用的时间 */
    moveUpTime: 0.2,
    height: 40,
    /** 高度偏移 */
    deviation: 150,
  };
  private pushCenter(tips: Node) {
    if (!this.taskCenter || !this.taskCenter.isRunning) {
      this.taskCenter = Task.createSync();
    }
    this.taskCenter.add((next) => {
      this.moveUpCenter(next);
    });
    this.taskCenter.add((next) => {
      this.flyCenter(tips, next);
    });
    if (!this.taskCenter.isRunning) {
      this.taskCenter.start();
    }
  }

  private moveUpCenter(next) {
    const len = this.centerNodes.length;
    if (len > 0) {
      let j = len;
      for (let i = 0; i < len; i++, j--) {
        if (j == 1) {
          tween(this.centerNodes[i])
            .to(this.centerConfig.moveUpTime, { position: v3(0, j * this.centerConfig.height) })
            .call(next)
            .start();
        } else {
          tween(this.centerNodes[i])
            .to(this.centerConfig.moveUpTime, { position: v3(0, j * this.centerConfig.height) })
            .start();
        }
      }
    } else {
      next();
    }
  }

  private flyCenter(tips: Node, next) {
    if (tips == null) {
      next();
      return;
    }
    this.centerNodes.push(tips);
    this.node.addChild(tips);
    const uiOpacity = tips.getComponent(UIOpacity);
    Tween.stopAllByTarget(tips);
    Tween.stopAllByTarget(uiOpacity);
    uiOpacity.opacity = 255;
    tips.setScale(this.centerConfig.scale, this.centerConfig.scale);
    tips.setPosition(0, 0);
    tween(tips)
      .to(this.centerConfig.outTime, { scale: v3(1, 1, 1) }, { easing: 'backOut' })
      .call(() => {
        next();
      })
      .delay(this.centerConfig.holdTime)
      .call((tag) => {
        const uiO = tag.getComponent(UIOpacity);
        tween(uiO)
          .to(this.centerConfig.hideTime, { opacity: 0 })
          .call(() => {
            const temp = this.centerNodes.shift();
            if (temp) {
              this.factory.push(temp);
            }
            if (this.centerNodes.length == 0) {
              this.taskCenter = null;
            }
          })
          .start();
      })
      .start();
  }
}
